package com.stedu.controller;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.Admin;
import com.stedu.bean.Repairer;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.mapper.AdminMapper;
import com.stedu.service.AdminService;
import com.stedu.service.RepairerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/employee")
public class RepairerController {
    @Autowired
    private RepairerService repairerService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private  AdminService adminService;
    //查询所有
    @GetMapping("/selectAll")
    public RespBean selectAll() {
        List<Repairer> list = repairerService.selectAll();
        return RespBean.ok("", list);
    }

    @GetMapping
    public RespBean selectByPage(Integer pageNum, String rname) {
        if (pageNum == null) {
            pageNum = 1;
        }

        PageHelper.startPage(pageNum, 5);
        List<Repairer> repairerList = repairerService.selectByRname(rname);
        PageInfo<Repairer> pageInfo = new PageInfo<>(repairerList);

        return RespBean.ok("查询成功", pageInfo);
    }

    @PostMapping
    public RespBean insert(@Validated @RequestBody Repairer repairer) throws SQLException, SteduException {


        if (repairerService.insert(repairer)) {
            return RespBean.ok("添加成功");

        } else {
            return RespBean.error("添加失败，请重新添加");


        }
    }
    @PostMapping("/account")
    public RespBean insertAdmin(@Validated @RequestBody Admin admin) throws SQLException, SteduException {
        // 检查用户名是否已存在
        if (adminService.checkUsernameExist(admin)) {
            return RespBean.error("账号已存在，不能添加");
        }
        String md5pwd = SecureUtil.md5(SecureUtil.md5(admin.getPassword()));
        admin.setPassword(md5pwd);

        if (adminMapper.insertAdmin(admin)) {
            return RespBean.ok("添加成功");
        } else {
            return RespBean.error("添加失败，请重新添加");
        }
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public RespBean update( @Validated @RequestBody Repairer repairer) throws SQLException, SteduException {
        if (repairerService.update(repairer)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }
    //管理员修改员工密码
    @RequestMapping(value = "/Aupdate", method = RequestMethod.POST)
    public RespBean updateAdmin( @RequestBody Admin admin) throws SQLException, SteduException {
        String md5pwd = SecureUtil.md5(SecureUtil.md5(admin.getPassword()));
        admin.setPassword(md5pwd);
        // 假设在更新员工时也需要检查用户名是否存在（根据业务需求决定）
        if (adminService.checkUsernameExist(admin)) {
            return RespBean.error("用户名已存在，不能修改");
        }
        if (adminService.Aupdate(admin)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败");
        }
    }
    @GetMapping("/{rid}")
    public RespBean selectByRid(@PathVariable("rid") Integer rid) throws SQLException {
        Repairer r = repairerService.selectByRid(rid);
        return RespBean.ok("", r);
    }
    @DeleteMapping("/{rid}")
    public RespBean delete(@PathVariable("rid") Integer rid) throws SQLException, SteduException {
        if (repairerService.delete(rid)) {
            return RespBean.ok("成功离职~~~");
        } else {
            return RespBean.error("存在未完成的关联任务，不能离职该维修人员信息");
        }
    }

}

